package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DomainModel;
import models.OwnerDomainModel;

import data.DomainData;
import data.OwnerDomainData;

import admin.data.UserData;

public class UpdateOwnerDomainChangeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
    String strIds = (String) request.getParameter("ids");
    String action = (String) request.getParameter("action");
    int isUpdated = 0;
    String[] ids = strIds.split(",");
    UserData uData = new UserData();
    
    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("isupdate"));
    
    OwnerDomainData ownerData = new OwnerDomainData();
    DomainData domainData = new DomainData();
    for (int i = 0; i < ids.length; i++) {
        if (!ids[i].isEmpty()) {
        	if(statusAction.contains(action.toLowerCase())) {
        		isUpdated = 1;
                
                OwnerDomainModel ownerDomain = ownerData.getOwnerDomain(ids[i]);
                if (ownerDomain != null) {
                	//Update domain info
                	DomainModel domain = domainData.get(ownerDomain.getDomain());
                	boolean isChange = false;
                	if (!ownerDomain.getTitle().isEmpty()) {
                		isChange = true;
                		domain.setTitle(ownerDomain.getTitle());
                	}
                	
                	if (!ownerDomain.getDescription().isEmpty()) {
                		isChange = true;
                		domain.setDescription(ownerDomain.getDescription());
                	}
                	
                	if (isChange) {
                		domainData.insert(domain);
                	}
                	
                	//Update onwer domain
                	ownerDomain.setIsUpdated(isUpdated);
                	ownerData.insert(ownerDomain);
                }
        	} else if (action.equalsIgnoreCase("remove")) {
        		
			}
        }
    }
}
}
